﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Store.WP7.Data.WebStore;
using System.Globalization;

namespace Store.WP7.ViewModels
{
    public class ProductViewModel : ViewModelBase
    {
        private readonly Product _innerProduct;


        //public ProductViewModel() { }
        public ProductViewModel(Product p)
        {
            _innerProduct = p;
            Price = string.Format(new CultureInfo("fr-fr"), "{0:C}", _innerProduct.Price);

            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                PictureUrl = new BitmapImage(new Uri(_innerProduct.PictureUrl));
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    PictureUrl = new BitmapImage(new Uri(_innerProduct.PictureUrl));
                });
            }
        }

        public int Id
        {
            get
            {
                return _innerProduct.Id;
            }
            set
            {
                _innerProduct.Id = value;
                RaisePropertyChanged(() => Id);
            }
        }

        public string Title
        {
            get
            {
                return _innerProduct.Title;
            }
            set
            {
                _innerProduct.Title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        public string Description
        {
            get { return _innerProduct.Description; }
            set
            {
                _innerProduct.Description = value;
                RaisePropertyChanged(() => Description);
            }
        }

        private string price;
        public string Price
        {
            get { return price; }
            set
            {
                if (price != value)
                {
                    price = value;
                    RaisePropertyChanged(() => this.Price);
                }
            }
        }

        private BitmapSource _pictureUrl;
        public BitmapSource PictureUrl
        {
            get
            {
                return _pictureUrl;
            }
            set
            {
                _pictureUrl = value;
                RaisePropertyChanged(() => PictureUrl);
            }
        }

        public Product InnetProduct
        {
            get { return _innerProduct; }
        }
    }
}
